﻿using CalOut;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevMy.Model;
using DevMy.PublicSetUp;
using MDIDemo.Model;
using MDIDemo.PublicClass;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DevMy.Vou
{
    public partial class Form_EarthReport : Form_BaseAlone
    {
        public Form_EarthReport(string FormText, Form_NewMain form_NewMain)
        {
            InitializeComponent();
            SetComponent(FormText);
            this.form_NewMain = form_NewMain;

        }
        private Form_NewMain form_NewMain;
        private WaitDialogForm waitDialogForm;
        private Class_MySqlDataBase class_MySqlDataBase, class_MySqlDataBaseOld;
        private Class_LookUpEdit class_LookUpEdit;
        private string WhereString = null;
        private bool BeginSgin = false;
        private string Separation = null;
        private bool IsBatchId = false;
        private bool IsEquipmentId = false;
        private bool IsCount = false;
        private string batchId = null;
        private string equipmentId = null;
        string calType = null;

        private void ChangeDate(int Index)
        {
            DateTime dateTime = DateTime.Now.AddMonths(-1 * Index);
            this.dateEdit1.DateTime = dateTime.AddDays(-1 * dateTime.Day + 1);
            this.dateEdit2.DateTime = dateTime.AddMonths(1).AddDays(-1 * dateTime.Day);
        }
        private string GetSeparation(int Index)
        {
            DateTime dateTime = DateTime.Now.AddMonths(-1 * Index);
            return dateTime.ToString("yyyyMM");
        }
        protected void SetComponent(string FormText)
        {
            Class_PublicMethod class_PublicMethod = new Class_PublicMethod();
            Class_DataBaseConDefault class_DataBaseConDefault = new Class_DataBaseConDefault();
            class_DataBaseConDefault = class_PublicMethod.FromXmlToDefaultValueObject<Class_DataBaseConDefault>("DataBaseDefaultValues");

            class_MySqlDataBaseOld = new Class_MySqlDataBase(class_DataBaseConDefault.dataSourceUrl
                , class_DataBaseConDefault.dataBaseName
                , class_DataBaseConDefault.dataSourceUserName
                , class_DataBaseConDefault.dataSourcePassWord
                , class_DataBaseConDefault.Port);
            class_MySqlDataBase = new Class_MySqlDataBase(class_DataBaseConDefault.dataSourceUrl
                , "yantu"
                , class_DataBaseConDefault.dataSourceUserName
                , class_DataBaseConDefault.dataSourcePassWord
                , class_DataBaseConDefault.Port);
            LookUpEditColumnModel[] lookUpEditColumnModels = new LookUpEditColumnModel[2];
            lookUpEditColumnModels[0] = new LookUpEditColumnModel("equipmentName", "设备名称");
            lookUpEditColumnModels[1] = new LookUpEditColumnModel("id", "设备编号");
            DataTable dataTable = new DataTable();
            dataTable = class_MySqlDataBaseOld.GetDataTable(@"Select id
            , equipmentName
            From vou_equipment ORDER BY sortNo");
            class_LookUpEdit = new Class_LookUpEdit();
            class_LookUpEdit.SetLookUpEdit(this.lookUpEdit1, "equipmentName", "id", lookUpEditColumnModels, "设备名称");
            this.lookUpEdit1.Properties.DataSource = dataTable;
            if (dataTable != null && dataTable.Rows.Count > 0)
            {
                this.lookUpEdit1.ItemIndex = 0;
            }
            dataTable.Dispose();

            this.radioGroup1.Properties.Items.Clear();
            DateTime dateTime = DateTime.Now;
            for (int i = 0; i < 12; i++)
            {
                this.radioGroup1.Properties.Items.Add(
                    new DevExpress.XtraEditors.Controls.RadioGroupItem(null
                    , dateTime.AddMonths(-1 * i).ToString("yyyy-MM")));
            }
            this.radioGroup1.SelectedIndex = 0;
            Separation = GetSeparation(this.radioGroup1.SelectedIndex);
            ChangeDate(this.radioGroup1.SelectedIndex);

            base.SetComponent(FormText, "I0004");
            //base.SetViewNo();
            base.SetAdd("指定计算", "指定批次，重新计算。");
            //base.SetEdit("变参计算", "将指定参数改变后，重新计算。");
            //base.SetDelete("全部计算", "将未计算过的原始数据，全部计算。");
            base.SetEditNo();
            base.SetViewNo();
            base.SetDeleteNo();
            base.SetExcelNo();
            base.SetGridDataSource(null);
            base.GetGridPage().PageSize = 50;

            BeginSgin = true;
        }

        #region WORD
        //public override void ToWord(SaveFileDialog saveFileDialog)
        //{
        //    const string FileName = "导出WORD";
        //    string DataBaseFileName = string.Format("{0}{1}"
        //        , FileName
        //        , System.DateTime.Now.ToString("yyyyMMdd"));
        //    SaveFileDialog SaveFileDialog = new SaveFileDialog();
        //    SaveFileDialog.FileName = DataBaseFileName;
        //    SaveFileDialog.Filter = "WORD|*.docx";
        //    SaveFileDialog.Title = string.Format("导出{0}", FileName);
        //    if (SaveFileDialog.ShowDialog() == DialogResult.OK)
        //    {
        //        MessageBox.Show("未测试到有效的WORD版本！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        //    }
        //}
        #endregion

        public override void ToExcel(SaveFileDialog saveFileDialog)
        {
            #region
            //const string FileName = "设备基本信息";
            //string DataBaseFileName = string.Format("{0}{1}"
            //    , FileName
            //    , System.DateTime.Now.ToString("yyyyMMdd"));
            //SaveFileDialog SaveFileDialog = new SaveFileDialog();
            //SaveFileDialog.FileName = DataBaseFileName;
            //SaveFileDialog.Filter = "EXCEL|*.xlsx";
            //SaveFileDialog.Title = string.Format("导出{0}", FileName);
            //if (SaveFileDialog.ShowDialog() == DialogResult.OK)
            //{
            //    Class_ToExcel class_ToExcel = new Class_ToExcel();
            //    ArrayList SheetNameArray = new ArrayList();
            //    ArrayList SheetTitleArray = new ArrayList();
            //    ArrayList FieldTitleArray = new ArrayList();
            //    ArrayList AveFieldNameList = new ArrayList();
            //    bool IsTotal = false;
            //    bool IsAverage = false;
            //    bool IsHeader = false;
            //    string LeftFieldName = null;
            //    string ExcelFileName = DataBaseFileName;
            //    string SumFieldNameList = null;
            //    DataSet ExcelDataSet = new DataSet();

            //    #region 自定义
            //    SheetNameArray.Add(FileName);
            //    SheetTitleArray.Add(FileName);
            //    DataTable dataTable = new DataTable();
            //    dataTable = class_MySqlDataBase.GetEquipmentNoPage(1, 0);
            //    ExcelDataSet.Tables.Add(dataTable);
            //    #endregion

            //    class_ToExcel._GiveYouExcel(
            //        SheetNameArray
            //        , SheetTitleArray
            //        , IsTotal
            //        , ExcelFileName
            //        , FieldTitleArray
            //        , SumFieldNameList
            //        , IsAverage
            //        , AveFieldNameList
            //        , IsHeader
            //        , LeftFieldName
            //        , ExcelDataSet);

            //    ExcelDataSet.Dispose();
            //    if (SheetNameArray.Count > 0)
            //        SheetNameArray.Clear();
            //    if (SheetTitleArray.Count > 0)
            //        SheetTitleArray.Clear();
            //    if (FieldTitleArray.Count > 0)
            //        FieldTitleArray.Clear();
            //    if (AveFieldNameList.Count > 0)
            //        AveFieldNameList.Clear();
            //}
            #endregion
        }
        private double[,] GetTwoArrayDefaultValue(string paramName, int sensorNo, string mainId)
        {
            int len = 6;
            double[,] myResult = new double[len, len];
            string Sql = string.Format(@"SELECT defaultValue
FROM vou_equipmentparamer
WHERE sensorNo = {0}
AND mainId = {1}
AND paramName = '{2}'
ORDER BY sortNo"
                , sensorNo
                , mainId
                , paramName);
            DataTable dataTable = class_MySqlDataBaseOld.GetDataTable(Sql);
            if (dataTable != null && dataTable.Rows.Count > 0)
            {
                int Row = 0;
                int Col = 0;
                for (int i = 0; i < dataTable.Rows.Count; i++)
                {
                    Col = i / len;
                    Row = i % len;
                    myResult[Col, Row] = double.Parse(dataTable.Rows[i][0].ToString());
                }
            }
            dataTable.Clear();
            dataTable.Dispose();
            return myResult;
        }
        private double[] GetOneArrayDefaultValue(string paramName, int sensorNo, string mainId)
        {
            double[] myResult;
            string Sql = string.Format(@"SELECT defaultValue
FROM vou_equipmentparamer
WHERE sensorNo = {0}
AND mainId = {1}
AND paramName like '{2}%'
ORDER BY sortNo"
                , sensorNo
                , mainId
                , paramName);
            DataTable dataTable = class_MySqlDataBaseOld.GetDataTable(Sql);
            myResult = new double[dataTable.Rows.Count];
            if (dataTable != null && dataTable.Rows.Count > 0)
            {
                for (int i = 0; i < dataTable.Rows.Count; i++)
                    myResult[i] = double.Parse(dataTable.Rows[i][0].ToString());
            }
            dataTable.Clear();
            dataTable.Dispose();
            return myResult;
        }

        private double GetDefaultValue(string paramName, int sensorNo, string mainId)
        {
            double myResult = 0;
            string Sql = string.Format(@"SELECT defaultValue
FROM vou_equipmentparamer
WHERE sensorNo = {0}
AND mainId = {1}
AND paramName = '{2}'
ORDER BY sortNo"
                , sensorNo
                , mainId
                , paramName);
            DataTable dataTable = class_MySqlDataBaseOld.GetDataTable(Sql);
            if (dataTable != null && dataTable.Rows.Count == 1)
            {
                myResult = double.Parse(dataTable.Rows[0][0].ToString());
            }
            dataTable.Clear();
            dataTable.Dispose();
            return myResult;
        }

        /// <summary>
        /// 指定计算
        /// </summary>
        /// <returns></returns>
        public override int AddData()
        {
            //return base.AddData();
            //32, 31, 53
            bool IsOk = false;
            Form_Car1 form_Car1 = new Form_Car1();
            form_Car1.SetWinType(1);
            form_Car1.id = null;
            int Index = this.radioGroup1.SelectedIndex;
            DateTime dateTime = DateTime.Now.AddMonths(-1 * Index);

            form_Car1.Month = dateTime.ToString("yyyy-MM");
            form_Car1.Ini();
            form_Car1.Text = this.Text;
            if (form_Car1.ShowDialog() == DialogResult.OK)
            {
                #region 得到查找参数
                if (form_Car1.IsBatchId && (form_Car1.batchId == null || form_Car1.batchId.Length == 0))
                {
                    MessageBox.Show("请选择指定批次号！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return -1;
                }
                if (form_Car1.IsEquipmentId && (form_Car1.equipmentId == null || form_Car1.equipmentId.Length == 0))
                {
                    MessageBox.Show("请选择设备标识！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return -1;
                }
                this.IsBatchId = form_Car1.IsBatchId;
                this.IsEquipmentId = form_Car1.IsEquipmentId;
                this.IsCount = form_Car1.IsCount;
                this.batchId = form_Car1.batchId;
                this.equipmentId = form_Car1.equipmentId;
                this.calType = form_Car1.calType;
                IsOk = true;
                #endregion
            }
            form_Car1.Dispose();

            if (!IsOk)
                return -1;

            #region 得到任务总数
            int TaskMax = GetTaskCount();
            form_NewMain.SetProgressMax(TaskMax);
            #endregion

            #region 得到批次号并开始计算
            List<string> batchIdList = GetBatchIdList();
            if (batchIdList != null && batchIdList.Count > 0)
            {
                for (int batchIndex = 0; batchIndex < batchIdList.Count; batchIndex++)
                {
                    string nowBatchId = batchIdList[batchIndex];

                    #region 删除
                    DeleteOutData(nowBatchId);
                    #endregion

                    #region 得到指定批次数据
                    DataTable dataTable = GetBatchData(nowBatchId);
                    #endregion
                    if (dataTable != null && dataTable.Rows.Count % 7 == 0)
                    {
                        /*string TimeSign = this.Separation;*/
                        string id = dataTable.Rows[0]["id"].ToString();
                        //int ChannelNo = int.Parse(item["ChannelNo"].ToString());
                        int sortNo = int.Parse(dataTable.Rows[0]["sortNo"].ToString());
                        string mainId = dataTable.Rows[0]["equipmentId"].ToString();
                        string equipmentId = dataTable.Rows[0]["equipmentId"].ToString();
                        string projectId = "projectId";
                        mainId = "11"; equipmentId = "11";
                        double[] 传感器输出波长 = new double[7];

                        Class_Cal cal = new Class_Cal();
                        int InsertCount = 0;
                        if (calType == "正应力")
                        {
                            for (int k = 1; k < 7; k++)
                            {
                                string FieldName = string.Format("waveValue{0}", k);
                                int sensorNo = k;
                                double 温度补偿系数 = GetDefaultValue("temperatureCompensateParamer", sensorNo, mainId);
                                double 初始温度 = GetDefaultValue("iniTemperatureWave", sensorNo, mainId);
                                double[] 传感器初始波长 = GetOneArrayDefaultValue("iniNormalStressWave", sensorNo, mainId);
                                double[] 传感器标定系数 = GetOneArrayDefaultValue("normalStressCalibrationParamer", sensorNo, mainId);
                                double 方位角α = GetDefaultValue("angleα", sensorNo, mainId);
                                double 倾角β = GetDefaultValue("angleβ", sensorNo, mainId);
                                double 旋转角γ = GetDefaultValue("angleγ", sensorNo, mainId);
                                double[,] 应力变换矩阵 = GetTwoArrayDefaultValue("powerChangeMatrix", sensorNo, mainId);

                                #region 第一道
                                int index = 0;
                                double totalValue = 0;
                                for (int row = 0; row < 7; row++)
                                {
                                    传感器输出波长[row] = double.Parse(dataTable.Rows[index * 7 + row][FieldName].ToString());
                                    totalValue += double.Parse(dataTable.Rows[index * 7 + row][FieldName].ToString());
                                }

                                if ((Math.Abs(totalValue) > 0))
                                {
                                    cal.设置初始值(
                                        温度补偿系数
                                        , 初始温度
                                        , 传感器初始波长
                                        , 传感器标定系数
                                        , 方位角α
                                        , 倾角β
                                        , 旋转角γ
                                        , 传感器输出波长
                                        , 应力变换矩阵);
                                    batchId = nowBatchId;
                                    Cal应力_Result cal应力 = cal.Get应力(null, batchId, equipmentId, projectId);
                                    if (cal应力 != null)
                                    {
                                        int length = cal应力.正应力.Length;
                                        for (int i = 0; i < length; i++)
                                        {
                                            if (i == 0)
                                            {
                                                string InsertTemp = cal应力.Get温度Insert(batchId, sensorNo.ToString()
                                                    , 1
                                                    , cal应力.温度, string.Format("_{0}", this.Separation));
                                                InsertCount += class_MySqlDataBase.ExecuteSql(InsertTemp);
                                            }
                                            string InsertSql = cal应力.Get应力Insert(batchId
                                                , sortNo++
                                                , cal应力.正应力[i]
                                                , cal应力.局部坐标系应力[i]
                                                , cal应力.ONE次变换[i]
                                                , cal应力.TWO次变换[i]
                                                , cal应力.THREE次变换[i]
                                                , cal应力.大地坐标系应力[i]
                                                , cal应力.li1[i]
                                                , cal应力.pqθ[i]
                                                , cal应力.主应力123[i]
                                                , cal应力.li2[i]
                                                , cal应力.mi2[i]
                                                , cal应力.ni2[i]
                                                , cal应力.主应力倾角β[i]
                                                , cal应力.主应力方位角α[i]
                                                , string.Format("_{0}", this.Separation)
                                                , sensorNo.ToString());
                                            InsertCount += class_MySqlDataBase.ExecuteSql(InsertSql);
                                        }
                                    }
                                }
                                #endregion

                                #region 第二道
                                index++;
                                totalValue = 0;
                                if (dataTable.Rows.Count == 14)
                                {
                                    for (int row = 0; row < 7; row++)
                                    {
                                        传感器输出波长[row] = double.Parse(dataTable.Rows[index * 7 + row][FieldName].ToString());
                                        totalValue += double.Parse(dataTable.Rows[index * 7 + row][FieldName].ToString());
                                    }

                                    #region 验证数据是否大于0
                                    if ((Math.Abs(totalValue) > 0))
                                    {
                                        cal.设置初始值(
                                            温度补偿系数
                                            , 初始温度
                                            , 传感器初始波长
                                            , 传感器标定系数
                                            , 方位角α
                                            , 倾角β
                                            , 旋转角γ
                                            , 传感器输出波长
                                            , 应力变换矩阵);
                                        Cal应力_Result cal应力 = cal.Get应力(null, batchId, equipmentId, projectId);
                                        if (cal应力 != null)
                                        {
                                            int length = cal应力.正应力.Length;
                                            for (int i = 0; i < length; i++)
                                            {
                                                if (i == 0)
                                                {
                                                    string InsertTemp = cal应力.Get温度Insert(batchId, (sensorNo + 6).ToString()
                                                        , 1
                                                        , cal应力.温度, string.Format("_{0}", this.Separation));
                                                    InsertCount += class_MySqlDataBase.ExecuteSql(InsertTemp);
                                                }
                                                string InsertSql = cal应力.Get应力Insert(batchId
                                                    , sortNo++
                                                    , cal应力.正应力[i]
                                                    , cal应力.局部坐标系应力[i]
                                                    , cal应力.ONE次变换[i]
                                                    , cal应力.TWO次变换[i]
                                                    , cal应力.THREE次变换[i]
                                                    , cal应力.大地坐标系应力[i]
                                                    , cal应力.li1[i]
                                                    , cal应力.pqθ[i]
                                                    , cal应力.主应力123[i]
                                                    , cal应力.li2[i]
                                                    , cal应力.mi2[i]
                                                    , cal应力.ni2[i]
                                                    , cal应力.主应力倾角β[i]
                                                    , cal应力.主应力方位角α[i]
                                                    , string.Format("_{0}", this.Separation)
                                                    , (sensorNo + 6).ToString());
                                                InsertCount += class_MySqlDataBase.ExecuteSql(InsertSql);
                                            }
                                        }
                                    }
                                    #endregion
                                }
                                #endregion

                            }
                        }
                        else
                        {
                            for (int k = 1; k < 7; k++)
                            {
                                string FieldName = string.Format("waveValue{0}", k);
                                int sensorNo = k;
                                double 温度补偿系数 = GetDefaultValue("temperatureCompensateParamer", sensorNo, mainId);
                                double 初始温度 = GetDefaultValue("iniTemperatureWave", sensorNo, mainId);
                                double[] 传感器初始波长 = GetOneArrayDefaultValue("iniNormalStressWave", sensorNo, mainId);
                                double[] 传感器标定系数 = GetOneArrayDefaultValue("normalStressCalibrationParamer", sensorNo, mainId);

                                #region 第一道
                                int index = 0;
                                double totalValue = 0;
                                for (int row = 0; row < 7; row++)
                                {
                                    传感器输出波长[row] = double.Parse(dataTable.Rows[index * 7 + row][FieldName].ToString());
                                    totalValue += double.Parse(dataTable.Rows[index * 7 + row][FieldName].ToString());
                                }
                                if ((Math.Abs(totalValue) > 0))
                                {
                                    cal.设置初始值(
                                        温度补偿系数
                                        , 初始温度
                                        , 传感器初始波长
                                        , 传感器标定系数
                                        , 传感器输出波长);
                                    Cal应变_Result cal应变 = cal.Get应变(null, batchId, equipmentId, projectId);
                                    if (cal应变 != null)
                                    {
                                        int length = cal应变.正应力.Length;
                                        for (int i = 0; i < length; i++)
                                        {
                                            if (i == 0)
                                            {
                                                string InsertTemp = cal应变.Get温度Insert(batchId, sensorNo.ToString()
                                                    , 1
                                                    , cal应变.温度, string.Format("_{0}", this.Separation));
                                                InsertCount += class_MySqlDataBase.ExecuteSql(InsertTemp);
                                            }
                                            string InsertSql = cal应变.Get应变Insert(batchId, sensorNo.ToString()
                                                , sortNo++
                                                , cal应变.正应力[i], string.Format("_{0}", this.Separation));
                                            InsertCount += class_MySqlDataBase.ExecuteSql(InsertSql);
                                        }
                                    }
                                }
                                #endregion

                                #region 第二道
                                index++;
                                totalValue = 0;
                                if (dataTable.Rows.Count == 14)
                                {
                                    for (int row = 0; row < 7; row++)
                                    {
                                        传感器输出波长[row] = double.Parse(dataTable.Rows[index * 7 + row][FieldName].ToString());
                                        totalValue += double.Parse(dataTable.Rows[index * 7 + row][FieldName].ToString());
                                    }
                                    if ((Math.Abs(totalValue) > 0))
                                    {
                                        cal.设置初始值(
                                            温度补偿系数
                                            , 初始温度
                                            , 传感器初始波长
                                            , 传感器标定系数
                                            , 传感器输出波长);
                                        Cal应变_Result cal应变 = cal.Get应变(null, batchId, equipmentId, projectId);
                                        if (cal应变 != null)
                                        {
                                            int length = cal应变.正应力.Length;
                                            for (int i = 0; i < length; i++)
                                            {
                                                if (i == 0)
                                                {
                                                    string InsertTemp = cal应变.Get温度Insert(batchId, (sensorNo + 6).ToString()
                                                        , 1
                                                        , cal应变.温度, string.Format("_{0}", this.Separation));
                                                    InsertCount += class_MySqlDataBase.ExecuteSql(InsertTemp);
                                                }
                                                string InsertSql = cal应变.Get应变Insert(batchId, (sensorNo + 6).ToString()
                                                    , sortNo++
                                                    , cal应变.正应力[i], string.Format("_{0}", this.Separation));
                                                InsertCount += class_MySqlDataBase.ExecuteSql(InsertSql);
                                            }
                                        }
                                    }
                                }
                                #endregion
                            }
                        }

                        #region 累加计算数据
                        if (InsertCount > 0)
                            UpdateIniData(batchId);
                        #endregion

                    }
                    form_NewMain.SetProgressPosition(batchIndex + 1);
                    dataTable.Dispose();
                }
            }
            else
            {
                MessageBox.Show("未找到需要计算的数据！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            batchIdList.Clear();
            #endregion

            form_NewMain.SetProgressOK();
            return -1;
        }

        public int DeleteOutData(string BatchId)
        {
            if (BatchId != null)
            {
                string Sql = string.Format(@"DELETE FROM output_data_{1}
                    WHERE batchId = '{0}'"
                , BatchId, Separation);
                class_MySqlDataBase.ExecuteSql(Sql);
                Sql = string.Format(@"DELETE FROM output_temperature_{1}
                    WHERE batchId = '{0}'"
                , BatchId, Separation);
                return class_MySqlDataBase.ExecuteSql(Sql);
            }
            else
                return -1;
        }
        public int UpdateIniData(string batchId)
        {
            if (batchId != null)
            {
                string Sql = string.Format(@"UPDATE vou_inidata_{1}
                    SET updateCount = updateCount + 1
                    WHERE batchId = '{0}'"
                , batchId, Separation);
                return class_MySqlDataBaseOld.ExecuteSql(Sql);
            }
            else
                return -1;
        }

        public DataTable GetBatchData(string batchId)
        {
            string Sql = string.Format(@"SELECT *
                FROM vou_inidata_{1}
                WHERE batchId = '{0}'
                ORDER BY sortNo"
                , batchId, Separation);
            DataTable dataTable = class_MySqlDataBaseOld.GetDataTable(Sql);
            return dataTable;
        }
        public List<string> GetBatchIdList()
        {
            List<string> vs = new List<string>();
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendFormat(@"SELECT DISTINCT batchId as batchId
                FROM vou_inidata_{0}
                WHERE dataSource > -1", Separation);
            if (IsBatchId && batchId.Length > 0)
            {
                stringBuilder.AppendFormat(" AND batchId='{0}'", batchId);
            }
            if (IsEquipmentId && equipmentId.Length > 0)
            {
                stringBuilder.AppendFormat(" AND equipmentId='{0}'", equipmentId);
            }
            if (IsCount)
            {
                stringBuilder.Append(" AND updateCount = 0");
            }
            stringBuilder.Append(" ORDER BY batchId");
            DataTable dataTable = class_MySqlDataBaseOld.GetDataTable(stringBuilder.ToString());
            stringBuilder.Length = 0;
            if (dataTable != null && dataTable.Rows.Count > 0)
            {
                for (int i = 0; i < dataTable.Rows.Count; i++)
                {
                    vs.Add(dataTable.Rows[0][0].ToString());
                }
            }
            dataTable.Clear();
            dataTable.Dispose();
            return vs;

        }
        public int GetTaskCount()
        {
            int Count = 0;
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendFormat(@"SELECT count(*) as counter
                FROM vou_inidata_{0}
                WHERE dataSource > -1", Separation);
            if (IsBatchId && batchId.Length > 0)
            {
                stringBuilder.AppendFormat(" AND batchId='{0}'", batchId);
            }
            if (IsEquipmentId && equipmentId.Length > 0)
            {
                stringBuilder.AppendFormat(" AND equipmentId='{0}'", equipmentId);
            }
            if (IsCount)
            {
                stringBuilder.Append(" AND updateCount = 0");
            }
            DataTable dataTable = class_MySqlDataBaseOld.GetDataTable(stringBuilder.ToString());
            stringBuilder.Length = 0;
            if (dataTable != null && dataTable.Rows.Count == 1)
            {
                Count = int.Parse(dataTable.Rows[0][0].ToString());
            }
            dataTable.Clear();
            dataTable.Dispose();
            return Count;
        }
        /// <summary>
        /// 变参计算
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public override int EditData(object id)
        {
            //            DateTime dateTime = DateTime.Now;
            //          return SetCol("1600447372649", 11, dateTime, dateTime, true);
            Form_Car2 form_Car1 = new Form_Car2();
            form_Car1.SetWinType(1);
            form_Car1.id = null;
            form_Car1.Ini();
            form_Car1.Text = this.Text;
            if (form_Car1.ShowDialog() == DialogResult.OK)
            {
                MessageBox.Show("没有找到未计算的原始数据，无法重新计算！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            form_Car1.Dispose();

            return -1;
        }
        /// <summary>
        /// 全部计算
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public override int DeleteData(object id)
        {
            //            return base.DeleteData(id);
            MessageBox.Show("没有找到未计算过的原始数据！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            return -1;
        }
        public override void ToWord(SaveFileDialog saveFileDialog)
        {
            //base.ToWord(saveFileDialog);
            MessageBox.Show("未检测到有效的WORD版本！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        public override void Finder(int PageNo)
        {
            waitDialogForm = new WaitDialogForm("正在玩命加载中......", "温馨提示");

            WhereString = string.Format(@"WHERE createTime < '{1}'
                AND createTime > '{0}'"
                , this.dateEdit1.DateTime.AddDays(-1).ToString("yyyy-MM-dd")
                , this.dateEdit2.DateTime.AddDays(1).ToString("yyyy-MM-dd"));
            if (this.lookUpEdit1.ItemIndex > -1)
                WhereString = WhereString + string.Format(" AND address = {0}", this.lookUpEdit1.EditValue.ToString());
            DataSet dataSet = this.GetFinder(PageNo, base.GetGridPage().PageSize, WhereString);
            base.SetGridDataSource(dataSet.Tables[0]);
            base.GetGridPage().SetRowCount(int.Parse(dataSet.Tables[1].Rows[0][0].ToString()));
            waitDialogForm.Close();
        }
        private DataSet GetFinder(int PageNo, int PageSize, string Address)
        {
            #region
            string SelectSql = @"Select id
                ,batchId
                ,equipmentId
                ,address
                ,sortNo
                ,createTime
                ,waveValue1
                ,powerValue1
                ,waveValue2
                ,powerValue2
                ,waveValue3
                ,powerValue3
                ,waveValue4
                ,powerValue4
                ,waveValue5
                ,powerValue5
                ,waveValue6
                ,powerValue6
                ,updateCount";
            #endregion

            string FormSql = string.Format(@" From vou_inidata_{0}", Separation);
            //string FormSql = @" From vou_inidata";
            string WhereSql = Address;
            string GroupSql = null;
            string HavingSql = null;
            string OrderSql = @" ORDER BY createTime desc ,sortNo";
            string TotalFieldName = "*";

            PageSqlModel pageSqlModel = class_MySqlDataBaseOld.GetSql(SelectSql
            , FormSql
            , WhereSql
            , GroupSql
            , HavingSql
            , OrderSql
            , PageNo
            , PageSize
            , TotalFieldName);

            return class_MySqlDataBaseOld.GetPageDataSet(pageSqlModel);
        }


        private void Form_EarthReport_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (waitDialogForm != null)
            {
                waitDialogForm.Dispose();
            }
            class_MySqlDataBase.CloseSqlConnection();
            class_MySqlDataBaseOld.CloseSqlConnection();
        }

        private void gridPage1_RowCellClickEventOut(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            GridView gridView = sender as GridView;
            if (e.Column.FieldName == "batchId")
            {
                string batchId = e.CellValue.ToString();
                if (batchId != null)
                {
                    Form_ResultCal form_ResultCal = new Form_ResultCal();
                    form_ResultCal.BatchId = batchId;
                    form_ResultCal.SetWinType(-1);
                    form_ResultCal.IsAuto = true;
                    if (form_ResultCal.ShowDialog() == DialogResult.OK)
                    {

                    }
                    form_ResultCal.Close();
                    form_ResultCal.Dispose();
                }
            }
        }

        private void radioGroup1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (BeginSgin)
            {
                RadioGroup radioGroup = sender as RadioGroup;
                ChangeDate(radioGroup.SelectedIndex);
                Separation = GetSeparation(radioGroup.SelectedIndex);
            }
        }
    }
}
